2122. 还原原数组
为保证权益,题目请参考 2122. 还原原数组(From LeetCode).
解决方案1
Python
python
# 5966. 还原原数组
# https://leetcode-cn.com/problems/recover-the-original-array/
from typing import List
class Solution:
def recoverArray(self, nums: List[int]) -> List[int]:
nums.sort()
nd = {}
for num in nums:
if num in nd:
nd[num] += 1
else:
nd[num] = 1
nd_keys = sorted(list(nd.keys()).copy())
start = nums[0]
for i in range(1, len(nums)):
if (nums[i] - start) % 2 == 1 or nums[i] == start:
continue
m = nums[i] - start
ndc = nd.copy()
ans = []
notOk = False
for ndk in nd_keys:
while ndc[ndk] != 0:
if ndk + m in ndc and ndc[ndk + m] != 0:
ndc[ndk + m] -= 1
ndc[ndk] -= 1
ans.append(ndk + m // 2)
else:
notOk = True
break
if notOk:
break
if notOk:
continue
else:
return ans
return []
if __name__ == "__main__":
solution = Solution()
print(solution.recoverArray([5,435]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51